package JZOffer;

// https://leetcode.cn/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/?envType=study-plan&id=lcof&plan=lcof&plan_progress=xxj1ozje
// 合并两个排序的链表
public class Day012_1 {
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode node = new ListNode(-1);
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        ListNode cur = node;

        while (cur1 != null && cur2 != null) {
            if (cur1.val > cur2.val) {
                cur.next = cur2;
                cur2 = cur2.next;
            } else {
                cur.next = cur1;
                cur1 = cur1.next;
            }
            cur = cur.next;
        }

        if (cur1 == null) {
            cur.next = cur2;
        } else {
            cur.next = cur1;
        }
        return node.next;
    }

    public static void main(String[] args) {
        MySingleList list1= new MySingleList();
        MySingleList list2= new MySingleList();
        list1.addLast(1);
        list1.addLast(3);
        list1.addLast(5);
        list2.addLast(2);
        list2.addLast(4);
        list2.addLast(6);
        ListNode node = mergeTwoLists(list1.head, list2.head);
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }
}
